package com.allqj;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;

/**
 * @author sjw
 * @className Main
 * @date 2021/8/30 17:01
 * @description: TODO
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        WeakHashMap<String, User> map = new WeakHashMap<>();
        User user1=new User();
        User user2=new User();
        SoftReference<User> userSoftReference=new SoftReference<>(user1);
        WeakReference<User> userWeakReference=new WeakReference<>(user2);
        System.out.println("enter=============");
        user1=null;
        user2=null;
        if(userSoftReference.get()==null) System.out.println("1 userSoftReference.get()==null");
        if(userWeakReference.get()==null) System.out.println("1 userWeakReference.get()==null");
        System.gc();
        try {
            byte[] s = new byte[1024*1024];
        }catch (Exception e){
            System.out.println(1);
        }
        Thread.sleep(10000);
        if(userSoftReference.get()==null) System.out.println("2 userSoftReference.get()==null");
        if(userWeakReference.get()==null) System.out.println("2 userWeakReference.get()==null");
        System.out.println("end=============");
    }
    static class User{
        private String name="name";
        private Integer age;
    }
}
